package com.controller;

import com.pojo.User;
import com.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    /*
    用户登录共实现，需要验证用户名及密码的正确性；
    返回int
    1：管理员
    2：会员
    3：普通收银员
     */
    @RequestMapping(value = "log",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String Login(String username, String password, HttpSession session){
        User user = new User();
        user.setUserName(username);
        user.setUserPassword(password);
        User login = loginService.login(user);
        session.setAttribute("username",username);
        session.setAttribute("userid",login.getId());
        if (login!=null){
            if (login.getUserRole()==2){
                return "2";
            }
            if (login.getUserRole()==1){
                return "1";
            }
        }
        return "0";
    }
    /*
    用户注册功能实现，需要使用User表的字段
    详情可以@User JavaBean
    返回
    提示注册失败or成功
     */
    @RequestMapping(value = "register",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String Register(String userName,String Password,Integer gender,String birthday,String phone,String address){
        User user = new User();
        user.setUserName(userName); user.setUserPassword(Password); user.setGender(gender);
        user.setBirthday(birthday); user.setPhone(phone); user.setAddress(address);
        int register = loginService.register(user);
        if (register==-1){
            return "您已有账号，请勿重复操作";
        }
        if (register>0){
            return "注册成功";
        }
        return "注册失败";
    }


    /*
     * 加载了用户主页
     * 主要用于管理员登录后得使用，进入其他界面的操作
     */
    @RequestMapping("adminIndex")
    public ModelAndView AdminIndex(){
        return new ModelAndView("admin/index");
    }
    //这里是加载了表单组件测试界面
    @RequestMapping("fromtest")
    public ModelAndView FromTest(){
        return new ModelAndView("admin/formtest");
    }
    //加载了数据表页面
    @RequestMapping("charts")
    public ModelAndView Charts(){
        Integer dataAll = loginService.dataAll();
        Integer data1 = loginService.data1();
        Integer data2 = loginService.data2();
        Integer data3 = loginService.data3();
        Integer data4 = loginService.data4();
        ModelAndView mv= new ModelAndView("admin/charts");
        mv.addObject("dataAll",dataAll);
        mv.addObject("data1",data1);
        mv.addObject("data2",data2);
        mv.addObject("data3",data3);
        mv.addObject("data4",data4);
        return mv;

    }
}
